﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
using BHair.Base;

namespace BHair
{
    public partial class frmzjkf : Form
    {
        public frmzjkf()
        {
            InitializeComponent();
        }
        string constr = ConfigurationManager.ConnectionStrings["BHair.Properties.Settings.BHairDBConnectionString"].ConnectionString;
        private void button1_Click(object sender, EventArgs e)
        {
            if (this.txtcardid.Text != string.Empty)
            {
                chuanzhi ca = new chuanzhi();
                if (ca.checkpwdifnull(txtcardid.Text) == false)
                {
                    if (ca.CheckPass(txtcardid.Text) == true)
                    {
                        if (ca.Checkyxq(txtcardid.Text) == true)
                        {

                            selectcard();
                        }
                        else
                        {
                            MessageBox.Show("此卡已过有效期");
                            txtcardid.Text = null;
                        }
                    }
                    else
                    {
                        MessageBox.Show("卡号不存在！");
                        txtcardid.Text = null;
                    }
                }
                else
                {
                    chuanzhi.cardidpp = txtcardid.Text;
                    frmcheckpwd pwd = new frmcheckpwd();
                    pwd.ShowDialog();
                    if (chuanzhi.ifpwd == 1)
                    {
                        if (ca.CheckPass(txtcardid.Text) == true)
                        {
                            if (ca.Checkyxq(txtcardid.Text) == true)
                            {

                                selectcard();
                            }
                            else
                            {
                                MessageBox.Show("此卡已过有效期");
                                txtcardid.Text = null;
                            }
                        }
                        else
                        {
                            MessageBox.Show("卡号不存在！");
                            txtcardid.Text = null;
                        }
                    }
                    else
                    {
                        txtcardid.Text = null;
                    }

                }
                

            }
            else
            {
                MessageBox.Show("请输入卡号");
            }


        }
        public void selectcard() 
        {
            string cardID = this.txtcardid.Text;//获取卡号
            string cardname="";//姓名
            string phone="";//电话
            decimal yue=0;//余额
            SqlConnection conn = new SqlConnection(constr);
            string sql1 = string.Format("SELECT [mname],[mphone],[mbalance] FROM [BHairDB].[dbo].[member] where mid='{0}'",cardID);
            conn.Open();
            SqlCommand comm1 = new SqlCommand(sql1,conn);
            SqlDataReader sdr = comm1.ExecuteReader();
            while (sdr.Read()) 
            {
                cardname = sdr["mname"].ToString().Trim();
                phone = sdr["mphone"].ToString().Trim();
                yue =decimal.Round(decimal.Parse( sdr["mbalance"].ToString().Trim()),2);
            }
            this.txtname.Text = cardname;
            this.txtphone.Text = phone;
            this.txtyue.Text = yue.ToString();

        }

        private void txtcardid_KeyDown(object sender, KeyEventArgs e)
        {
            chuanzhi ca = new chuanzhi();
                if (e.KeyCode == Keys.Enter)
                {
                if (this.txtcardid.Text != string.Empty)
                {
                   // chuanzhi ca = new chuanzhi();
                    if (ca.checkpwdifnull(txtcardid.Text) == false)
                    {
                        if (ca.CheckPass(txtcardid.Text) == true)
                        {
                            if (ca.Checkyxq(txtcardid.Text) == true)
                            {

                                selectcard();
                            }
                            else
                            {
                                MessageBox.Show("此卡已过有效期");
                                txtcardid.Text = null;
                            }
                        }
                        else
                        {
                            MessageBox.Show("卡号不存在！");
                            txtcardid.Text = null;
                        }
                    }
                    else
                    {
                        chuanzhi.cardidpp = txtcardid.Text;
                        frmcheckpwd pwd = new frmcheckpwd();
                        pwd.ShowDialog();
                        if (chuanzhi.ifpwd == 1)
                        {
                            if (ca.CheckPass(txtcardid.Text) == true)
                            {
                                if (ca.Checkyxq(txtcardid.Text) == true)
                                {

                                    selectcard();
                                }
                                else
                                {
                                    MessageBox.Show("此卡已过有效期");
                                    txtcardid.Text = null;
                                }
                            }
                            else
                            {
                                MessageBox.Show("卡号不存在！");
                                txtcardid.Text = null;
                            }
                        }
                        else
                        {
                            txtcardid.Text = null;
                        }

                    }


                }
                else
                {
                    MessageBox.Show("请输入卡号");
                }
            }
            
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string cardID = this.txtcardid.Text;   //卡号
            decimal kkje =decimal.Parse( this.txtje.Text);  //扣款金额
            decimal ye=decimal.Round(decimal.Parse(this.txtyue.Text),2);    //此前余额
            if (kkje <= ye)
            {
                decimal yue = ye - kkje;    //当前余额

                string time = DateTime.Now.ToLongDateString();
                //MessageBox.Show(yue.ToString());

                //将单据插入 数据库
                string sql1 = string.Format("INSERT INTO [BHairDB].[dbo].[pays]"
                    + "           ([pid]"
                    + "           ,[pmid]"
                    + "           ,[peid1]"
                    + "           ,[peid2]"
                    + "           ,[peid3]"
                    + "           ,[pmoney]"
                    + "           ,[pcash]"
                    + "           ,[pzero]"
                    + "           ,[ptype]"
                    + "           ,[pdate]"
                    + "           ,[premark]"
                    + "           ,[pstatus]"
                    + "           ,[pcall],[班组])"
                    + "     VALUES('{0}','{1}',0,0,0,{2},0,0,1,'{3}','{4}',1,0,'{5}')", this.lbdjh.Text, cardID, kkje.ToString(), time, this.txtbz.Text,chuanzhi.banzu);
                string sql2 = string.Format("INSERT INTO [BHairDB].[dbo].[paydetails]"
                    + "           ([pdpid]"
                    + "           ,[pdiid]"
                    + "           ,[pdnum]"
                    + "           ,[pbonus]"
                    + "           ,[pmoney],[班组])"
                    + "     VALUES('{0}',1000,1 ,1,{1},'{2}')", this.lbdjh.Text, kkje,chuanzhi.banzu);
                try
                {
                    SqlConnection conn1 = new SqlConnection(constr);
                    conn1.Open();
                    SqlCommand comm1 = new SqlCommand(sql1, conn1);
                    comm1.ExecuteNonQuery();
                    SqlConnection conn2 = new SqlConnection(constr);
                    conn2.Open();
                    SqlCommand comm2 = new SqlCommand(sql2, conn2);
                    comm2.ExecuteNonQuery();
                    koukuan();
                    MessageBox.Show("结算成功直接扣款" + kkje.ToString() + "元");
                    conn1.Close();
                    conn2.Close();
                }
                catch (Exception e2)
                {

                    MessageBox.Show(e2.Message);
                }
            }
            else 
            {
                MessageBox.Show("本次扣款金额不得超过"+ye.ToString()+"元");
            }
        }

        private void frmzjkf_Load(object sender, EventArgs e)
        {
            string bs = "KK-";
            string time = DateTime.Now.ToLongDateString();
            Random ran = new Random();
            int RandKey = ran.Next(10, 9999);
            string bh = bs + time + "-" + RandKey.ToString();
            this.lbdjh.Text = bh;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public void koukuan() 
        {
            string cardID = this.txtcardid.Text;   //卡号
            decimal kkje = decimal.Parse(this.txtje.Text);  //扣款金额
            decimal ye = decimal.Round(decimal.Parse(this.txtyue.Text), 2);    //此前余额
            decimal yue = ye - kkje;  
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            string sql1 = string.Format("UPDATE [BHairDB].[dbo].[member] SET [mbalance] = {0} WHERE mid='{1}'",yue,cardID);
            SqlCommand comm = new SqlCommand(sql1,conn);
            comm.ExecuteNonQuery();
            conn.Close();
        }
    }
}
